package org.adream.account.rest.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adream.account.entity.ResourceEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.ResourceSearchForm;
import org.adream.account.service.ResourceService;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/resource/")
@Controller
public class ResourceApiController {
	
	private final static Logger logger = LoggerFactory.getLogger(ResourceApiController.class);

	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 新增
	 * @param resource
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addResource", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> addResource(@RequestBody ResourceEntity resource,HttpServletRequest request) {
		if (resource == null || StringUtils.isEmpty(resource.getResName())) {
			return new ResultModel<ResourceEntity>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return resourceService.addResource(resource);
	}

	/**
	 * 删除  逻辑删除
	 * @param resId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteResource", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> deleteResource(String resId,HttpServletRequest request) {
		
		if (StringUtils.isEmpty(resId)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return resourceService.deleteResource(resId);
	}
	
	/**
	 * 删除   逻辑删除  批量删除
	 * @param resIds
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteResources", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> deleteResources(@RequestBody String[] resIds,HttpServletRequest request) {
		if(resIds == null || resIds.length == 0) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		} 
		for (String resId : resIds) {
			deleteResource(resId, request);
		}
		return new ResultModel<String[]>(ResultModel.SUCCESS, "批量删除Resource", null);
	}

	/**
	 * 更新
	 * @param resource
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateResource", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> updateResource(@RequestBody ResourceEntity resource,HttpServletRequest request) {
		if (resource == null || StringUtils.isEmpty(resource.getResName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return resourceService.updateResource(resource);
	}

	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "queryResourcesByDr", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<List<ResourceEntity>> queryResourcesByDr(Integer dr) {
		if(StringUtils.isEmpty(dr)) {
			return new ResultModel<List<ResourceEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<ResourceEntity> resources = resourceService.queryResourcesByDr(dr);
		if(Utils.isEmptyCollection(resources)) {
			return new ResultModel<List<ResourceEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<ResourceEntity>>(ResultModel.SUCCESS, "成功获取Resource", resources);
	}
	
	/**
	 * 
	 * @param dr  删除标志
	 * @param resName resource name
	 * @return
	 */
	@RequestMapping(value = "queryResByDrAndLikeResName",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<List<ResourceEntity>> queryResByDrAndLikeResName(int dr,String resName) {
		List<ResourceEntity> resources = resourceService.queryResByDrAndLikeResName(dr, StringUtils.trimWhitespace(resName));
		if(Utils.isEmptyCollection(resources)) {
			return new ResultModel<List<ResourceEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<ResourceEntity>>(ResultModel.SUCCESS, "成功查询到Resource", resources);
	}
	
	/**
	 * 模糊分页查询
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "selectPageReses", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> selectPageReses(@RequestBody ResourceSearchForm form) {
		if(form == null || form.getDr() == null) {
			logger.warn("查询失败,参数错误:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,参数有误", null);
		}
		//若pageNo或pageSize未传入值,直接代码写入
		if(form.getPageNo() == null) {
			form.setPageNo(Constant.ONE);
		}
		if(form.getPageSize() == null) {
			form.setPageSize(Constant.TEN);
		}
		return resourceService.selectPageReses(form.getDr(), form.getResName(), form.getPageNo(), form.getPageSize());
	}
	
	/**
	 * 
	 * @param resId resource id
	 * @return
	 */
	@RequestMapping(value = "queryResourceByResId", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<ResourceEntity> queryResourceByResId(String resId) {
		if(StringUtils.isEmpty(resId)) {
			return new ResultModel<ResourceEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		ResourceEntity resource = resourceService.queryResourceByResId(resId);
		if(resource == null) {
			return new ResultModel<ResourceEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<ResourceEntity>(ResultModel.SUCCESS, "成功获取Resource", resource);
	}
	
	/**
	 * @param resName resource name
	 * @return
	 */
	@RequestMapping(value = "checkResName")
	public void checkResName(String resName,HttpServletResponse response) {
		boolean result = resourceService.isValidResName(resName);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(String.valueOf(result));
		} catch (IOException e) {
			logger.warn("检查resName出错:)");
		} finally {
			if(out != null) {
				out.close();
			}
		}
	}
}